<template>
  <el-container>
    <br>
    <el-header height="150px">
      <el-form :inline="true" label-position="left">
        <el-row>
          <el-col :span="6">
            <el-form-item label="表名称">
              <el-input v-model="searchMap.table_name" placeholder="表名称" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="表描述">
              <el-input v-model="searchMap.table_comment" placeholder="表描述" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="实体类名称">
              <el-input v-model="searchMap.class_name" placeholder="实体类名称" size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="使用的模板">
              <el-input size="small" v-model="searchMap.tpl_category" placeholder="使用的模板（crud单表操作 tree树表操作）"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="创建者">
              <el-input size="small" v-model="searchMap.create_by" placeholder="创建者"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间">
              <el-input size="small" v-model="searchMap.create_time" placeholder="创建时间"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="更新者">
              <el-input size="small" v-model="searchMap.update_by" placeholder="更新者"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="更新时间">
              <el-input size="small" v-model="searchMap.update_time" placeholder="更新时间"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="备注">
              <el-input size="small" v-model="searchMap.remark" placeholder="备注"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" :push="12">
            <el-button type="primary" @click="fetchData()" size="medium">查询</el-button>
            <el-button type="primary" @click="handleEdit('')" size="medium">新增</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-header>
    <!--   <div >
      <el-row style="height: 45px;">
        <el-col :span="6" :offset="17"  >
          <el-button type="primary" @click="fetchData()" size="medium">查询</el-button>
          <el-button type="primary" @click="handleEdit('')" size="medium">新增</el-button>
        </el-col>
      </el-row>
    </div> -->
    <el-main>
      <el-table align="center" :data="list" border style="width: 100%;" height="340" :header-cell-style="{color:'#2f2f2f'}">
        <el-table-column header-align="center" prop="table_id" label="编号" width="80"></el-table-column>
        <el-table-column prop="table_name" label="表名称" width="80"></el-table-column>
        <el-table-column prop="table_comment" label="表描述" width="80"></el-table-column>
        <el-table-column prop="class_name" label="实体类名称" width="100"></el-table-column>
        <el-table-column prop="tpl_category" label="使用的模板" width="100"></el-table-column>
        <el-table-column prop="package_name" label="生成包路径" width="100"></el-table-column>
        <el-table-column prop="module_name" label="生成模块名" width="100"></el-table-column>
        <el-table-column prop="business_name" label="生成业务名" width="100"></el-table-column>
        <el-table-column prop="function_name" label="生成功能名" width="100"></el-table-column>
        <el-table-column prop="function_author" label="生成功能作者" width="110"></el-table-column>
        <el-table-column prop="gen_type" label="生成代码方式" width="110"></el-table-column>
        <el-table-column prop="gen_path" label="生成路径" width="80"></el-table-column>
        <el-table-column prop="options" label="其它生成选项" width="110"></el-table-column>
        <el-table-column prop="create_by" label="创建者" width="80"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="80"></el-table-column>
        <el-table-column prop="update_by" label="更新者" width="80"></el-table-column>
        <el-table-column prop="update_time" label="更新时间" width="80"></el-table-column>
        <el-table-column prop="remark" label="备注" width="80"></el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row.id)" type="text" size="small">修改</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="fetchData" @current-change="fetchData" :current-page="currentPage"
        :page-sizes="[5,10,20]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <el-dialog title="编辑" :visible.sync="dialogFormVisible" width="50%">
        <el-form label-width="100px" :inline="true">
          <el-form-item label="表名称">
            <el-input v-model="pojo.table_name" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="表描述">
            <el-input v-model="pojo.table_comment" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="实体类名称">
            <el-input v-model="pojo.class_name" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="使用的模板">
            <el-input v-model="pojo.tpl_category" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="生成包路径">
            <el-input v-model="pojo.package_name" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="生成模块名">
            <el-input v-model="pojo.module_name" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="生成业务名">
            <el-input v-model="pojo.business_name" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="生成功能名">
            <el-input v-model="pojo.function_name" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="生成功能作者">
            <el-input v-model="pojo.function_author" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="生成代码方式">
            <el-input v-model="pojo.gen_type" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="生成路径">
            <el-input v-model="pojo.gen_path" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="其它生成选项">
            <el-input v-model="pojo.options" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="创建者">
            <el-input v-model="pojo.create_by" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input v-model="pojo.create_time" size="medium" ></el-input>
          </el-form-item>
          <el-form-item label="更新者">
            <el-input v-model="pojo.update_by" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="更新时间">
            <el-input v-model="pojo.update_time" size="medium"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="pojo.remark" size="medium"></el-input>
          </el-form-item>

          <el-button type="primary" @click="handleSave()">保存</el-button>
          <el-button @click="dialogFormVisible = false">关闭</el-button>
        </el-form>
      </el-dialog>
    </el-main>
  </el-container>
</template>
<script>
  import tableApi from '@/api/table'
  export default {
    data() {
      return {
        list: [],
        total: 0, // 总记录数
        currentPage: 1, // 当前页
        pageSize: 10, // 每页大小
        searchMap: {}, // 查询条件
        dialogFormVisible: false, // 编辑窗口是否可见
        pojo: {}, // 编辑表单绑定的实体对象
        cityList: [], // 城市列表
        id: '' // 当前用户修改的ID
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        tableApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
          this.list = response.data.rows
          this.total = response.data.total
        })
        this.list.push({ ' table_id ': ' 6 ', 'table_name ' : ' 6666666 ' })
        this.list.push({ ' table_id ': ' 7 ', 'table_name ' : ' 7777777 ' })
      },
      handleSave() {
        tableApi.update(this.id, this.pojo).then(response => {
          this.$message({
            message: response.message,
            type: (response.flag ? 'success' : 'error')
          })
          if (response.flag) { // 如果成功
            this.fetchData() // 刷新列表
          }
        })
        this.dialogFormVisible = false // 关闭窗口
      },
      handleEdit(id) {
        this.id = id
        this.dialogFormVisible = true // 打开窗口
        if (id !== '') { // 修改
          tableApi.findById(id).then(response => {
            if (response.flag) {
              this.pojo = response.data
            }
          })
        } else {
          this.pojo = {} // 清空数据
        }
      },
      handleDelete(id) {
        this.$confirm('确定要删除此纪录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          tableApi.deleteById(id).then(response => {
            this.$message({
              message: response.message,
              type: (response.flag ? 'success' : 'error')
            })
            if (response.flag) {
              this.fetchData() // 刷新数据
            }
          })
        })
      }
    }
  }
</script>
<style>
  .el-header {
    overflow-y: hidden;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    /* line-height: 160px; */
  }

  body>.el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  .el-col {
    text-align: right;
    height: 50px;
  }

  .el-row {
    margin-top: 2px;
    margin-bottom: 2px;
  }

  .el-table__header tr,
  .el-table__header th {
    padding: 0;
    line-height: 60px;
  }

  .el-table__body tr,
  .el-table__body td {
    padding: 0;
    height: 40px;
  }

  .el-pagination {
    height: 10px;
  }
</style>
